﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Home.Domain.Entities.Example;

namespace Home.WebUI.Areas.Example.Controllers
{
    public class Default1Controller : Controller
    {
        private ExampleDbContext db = new ExampleDbContext();

        //
        // GET: /Example/Default1/

        public ActionResult Index()
        {
            var userinfos = db.UserInfos.Include(u => u.Career);
            return View(userinfos.ToList());
        }

        //
        // GET: /Example/Default1/Details/5

        public ActionResult Details(int id = 0)
        {
            UserInfo userinfo = db.UserInfos.Find(id);
            if (userinfo == null)
            {
                return HttpNotFound();
            }
            return View(userinfo);
        }

        //
        // GET: /Example/Default1/Create

        public ActionResult Create()
        {
            ViewBag.CareerId = new SelectList(db.Careers, "Id", "Title");
            return View();
        }

        //
        // POST: /Example/Default1/Create

        [HttpPost]
        public ActionResult Create(UserInfo userinfo)
        {
            if (ModelState.IsValid)
            {
                db.UserInfos.Add(userinfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CareerId = new SelectList(db.Careers, "Id", "Title", userinfo.CareerId);
            return View(userinfo);
        }

        //
        // GET: /Example/Default1/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserInfo userinfo = db.UserInfos.Find(id);
            if (userinfo == null)
            {
                return HttpNotFound();
            }
            ViewBag.CareerId = new SelectList(db.Careers, "Id", "Title", userinfo.CareerId);
            return View(userinfo);
        }

        //
        // POST: /Example/Default1/Edit/5

        [HttpPost]
        public ActionResult Edit(UserInfo userinfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(userinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CareerId = new SelectList(db.Careers, "Id", "Title", userinfo.CareerId);
            return View(userinfo);
        }

        //
        // GET: /Example/Default1/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserInfo userinfo = db.UserInfos.Find(id);
            if (userinfo == null)
            {
                return HttpNotFound();
            }
            return View(userinfo);
        }

        //
        // POST: /Example/Default1/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserInfo userinfo = db.UserInfos.Find(id);
            db.UserInfos.Remove(userinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}